package com.wande.dataplatform.filecollection.transform.impl;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.wande.dataplatform.filecollection.transform.ITransformFunction;
import org.springframework.stereotype.Component;

/**
 * 正则替换函数
 *
 * @author wande
 */
@Component
public class RegexReplaceFunction implements ITransformFunction {

    @Override
    public String getName() {
        return "REGEX_REPLACE";
    }

    @Override
    public String getDescription() {
        return "正则表达式替换，参数: {\"pattern\": \"正则表达式\", \"replacement\": \"替换文本\"}";
    }

    @Override
    public Object transform(Object value, String params) {
        if (value == null) {
            return null;
        }

        String str = value.toString();
        
        // 解析参数
        if (params == null) {
            return str;
        }
        
        JSONObject json = JSONUtil.parseObj(params);
        String pattern = json.getStr("pattern");
        String replacement = json.getStr("replacement", "");

        if (pattern == null) {
            return str;
        }

        return str.replaceAll(pattern, replacement);
    }
}
